﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Globalization;

namespace FjordagerGymnastikHvorBorMedlemmerne
{
    using System;
    /// <summary>
    ///  found at http://oiorest.dk/danmark/documentation/eksempler.aspx
    ///  sample : http://oiorest.dk/danmark/wgs84koordinater?east=725868,932&north=6176694,058
    /// </summary>
    public class MercatorProjection
    {
        public double[] ETRS89ToWGS84(double northing, double easting)
        {
            string north = northing.ToString();
            string east = easting.ToString();
            north = north.Replace(',', '.');
            east = east.Replace(',', '.');
            string query = "http://oiorest.dk/danmark/wgs84koordinater?east=" + east + "&north=" + north;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(query);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            byte[] buf = new byte[8192];
            StringBuilder sb = new StringBuilder();

            double[] res = new double[2];
            // we will read data via the response stream
            System.IO.Stream resStream = response.GetResponseStream();
            string tempString = null;
            int count = 0;

            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.ASCII.GetString(buf, 0, count);
                    string lat = tempString.Substring(tempString.IndexOf("<latitude>") + 10, tempString.IndexOf("</latitude>") - (tempString.IndexOf("<latitude>") + 10));
                    string longitude = tempString.Substring(tempString.IndexOf("<longitude>") + 11, tempString.IndexOf("</longitude>") - (tempString.IndexOf("<longitude>") + 11));

                    //handling the issue that some uses US dot seperation and some uses comma seperation
                    if (System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.ToString() == ".")
                    {
                        res[0] = Convert.ToDouble(lat);
                        res[1] = Convert.ToDouble(longitude);
                    }
                    else
                    {
                        res[0] = Convert.ToDouble(lat.Replace('.',','));
                        res[1] = Convert.ToDouble(longitude.Replace('.',','));
                    }
                    
                    return res;
                }
            }
            while (count > 0); // any more data to read?
            return res;
        }
        public double[] WGS84ToETRS89(double latitude, double longtude)
        {
            throw new NotImplementedException();
        }
    }
}
